package com.dkt.api.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dkt.api.web.dto.ExceptionMsg;

@ControllerAdvice
public class GlobalExceptionHandler {

	// /**
	// * 出现异常时用于返回页面
	// *
	// * @param req
	// * @param e
	// * @return
	// */
	// @ExceptionHandler(value = Throwable.class)
	// public ModelAndView defaultErrorHandler(Throwable e) {
	// ModelAndView mav = new ModelAndView();
	// mav.addObject("exception", e);
	// mav.setViewName("error");
	// return mav;
	// }

	/**
	 * 出现异常时用于返回Json数据
	 * 
	 * @param req
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = Throwable.class)
	@ResponseBody
	public ExceptionMsg<String> jsonErrorHandler(Throwable e) {
		ExceptionMsg<String> r = new ExceptionMsg<>();
		r.setMessage(e.getMessage());
		r.setCode(ExceptionMsg.ERROR);
		r.setData("Some Data");
		return r;
	}

}
